package homework4.com.mybatis.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Service;

import homework4.com.mybatis.pojo.Account;
import homework4.com.mybatis.service.IAccountService;

@Service("accountService")
public class AccountServiceImpl implements IAccountService {
	@Override
	public void update(Account account) {
		try {
			InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			SqlSession session = sqlSessionFactory.openSession();
			session.update("homework4.com.mybatis.dao.AccountMapper.updateAccount", account);
			session.commit();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public Account findById(Integer id) {
		Account account = null;
		try {
			InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			SqlSession session = sqlSessionFactory.openSession();
			account = session.selectOne("homework4.com.mybatis.dao.AccountMapper.findAccountById",id);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return account;
	}

	@Override
	public void add(Account account) {
		try {
			InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			SqlSession session = sqlSessionFactory.openSession();
			session.update("homework4.com.mybatis.dao.AccountMapper.addAccount", account);
			session.commit();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void deleteById(Integer id) {
		try {
			InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			SqlSession session = sqlSessionFactory.openSession();
			session.update("homework4.com.mybatis.dao.AccountMapper.deleteAccountById", id);
			session.commit();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public List<Account> findAll() {
		List<Account> accounts = null;
		try {
			InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			SqlSession session = sqlSessionFactory.openSession();
			accounts = session.selectList("homework4.com.mybatis.dao.AccountMapper.findAllAccount");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return accounts;
	}

}
